<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>03 cookie</title>
</head>

<body>


    <script>
        //最简单的cookie 的方式
        document.cookie = 'kky=kisskillyou';
        document.cookie = 'kky1=kisskillyou1';

        //获取cookie
        var r1 = document.cookie;
        console.log('r1:', r1, Object.prototype.toString.call(r1));

        //
        var d = new Date().getTime();
        console.log('d:', d);

        var lostTime = new Date(d + 1 * 1000 * 60);
        document.cookie = 'name=xiaohei;expires=' + lostTime.toUTCString() + ';path=/';

        function setCookie(cname, cvalue, exdays) {
            var d = new Date();
            d.setTime(d.getTime() + (exdays * 1000 * 60 * 60 * 24));
            var expires = 'expires=' + d.toUTCString();
            document.cookie = cname + '=' + cvalue + ';' + expires;
        }

        setCookie('stars', '小黑', 7);

        function getCookie(cname) {
            var name = cname + '=';
            var arrCookie = document.cookie.split(';');
            for (var i = 0; i < arrCookie.length; i++) {
                var eValue = arrCookie[i].trim();
                console.log('eValue:', eValue);
                if (eValue.indexOf(name) != -1) {
                    var result = eValue.substring(name.length, eValue.length);
                    return result;
                }
            }
        }
        var result = getCookie('stars');
        console.log('result:', result);

        getCookie('kky');

        console.log(document.cookie);
    </script>
</body>

</html>